<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加身份</title>
	<link rel="stylesheet" href="/admin/src/css/layui.css">
	
</head>

<body >
	<form class="layui-form " style="margin:20px auto;">
		<div class="layui-form-item">
			<label class="layui-form-label">身份名称</label>
			<div class="layui-input-inline">
				<input type="text" name="role_name" id="role_name" lay-verify="required" placeholder="请输入身份名称" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">身份状态</label>
		    <div class="layui-input-block">
				<input type="radio" name="status" value="1" title="正常" checked>
				<input type="radio" name="status" value="0" title="隐藏">
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">身份权限</label>
		    <div id="powerList" class="demo-tree-more layui-input-inline"></div>
		</div>
		
	  <div class="layui-form-item">
	    <div class="layui-input-block">
	      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
	    </div>
	  </div>
	</form>
	<script src="/admin/src/layui.js" ></script>
<script>
	
    layui.use(['form','jquery','tree'], function(){
      var form = layui.form ,
		  layer = layui.layer,
		  tree = layui.tree,
		  $= layui.jquery;
		$.ajax({
			url : '/admin/role/add/form',
			type : 'get',
			dataType : 'json',
			contentType :"application/json",
			data : {'getTree':'getTree'},
			success : function (backData) {
			  if(backData['status'] == 200){
				  var formaData = backData['data']
				  //基本演示
					tree.render({
					  elem: '#powerList'
					  ,data: formaData
					  ,showCheckbox: true  //是否显示复选框
					  ,id: 'powerListId'
					  ,isJump: false //是否允许点击节点时弹出新窗口跳转
					  ,click: function(obj){
							var data = obj.data;  //获取当前点击的节点数据
							//layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
					  }
					});
			  }
			}
		})
	
      //监听表单提交
      form.on('submit(formDemo)', function(data){
		var getData = data.field;
		var checkedData = JSON.stringify(tree.getChecked('powerListId')); //获取选中节点的数据
		//console.log(checkedData);
		$.ajax({
			url: '/admin/role/add/data',
			type: 'POST',
			data:getData,
			success : function (backData) {
				//登录成功
			  if(backData['status'] == 200){
				  layer.msg(backData.message,{time:1200},function(){
					  window.parent.frames.location.href="/admin/role/index"
				  })
				  
			  }else{
				  layer.msg(backData.message);
			  }
			}
		})
        return false;
      });
    });

</script>
</body>
</html>
